import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import styles from './register.module.css';

import logo1 from './avatars/logo1.png'
import logo2 from './avatars/logo2.png'
import wechat from './avatars/wechat.png'
import axios from 'axios';
import { message } from 'antd';


export default function Register() {
  const [formData, setFormData] = useState({
    username: '',
    email: '',
    password: '',
    confirmPassword: ''
  });

  const navigate = useNavigate();

  const handleInputChange = (e) => {
    const { name, value } = e.target;
    setFormData(prev => ({
      ...prev,
      [name]: value
    }));
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    // 这里添加注册逻辑
    if (formData.password !== formData.confirmPassword) {
      alert('密码不一致');
      return;
    } else {
      let res = await axios.post('http://localhost:3000/register', formData);
      if (res.data.code === 200) {
        // 注册成功
        message.success('注册成功');
        navigate('/login');        
        console.log(formData);
        // 清空表单
        setFormData({
          username: '',
          email: '',
          password: '',
          confirmPassword: ''
        })
        // 注册成功后跳转到登录页
        
      }
    }
  };

  return (
    <div className={styles.container}>
      <div className={styles.leftSection}>
        <div className={styles.logo}>
          <img src={logo1} alt="Koto" />
          <h1>Koto</h1>
        </div>
        <h2>以最佳的方式管理您的文件存储</h2>
        <p>太棒了，我们为您创建了存储所有文档的完美场所。</p>
        <div className={styles.illustration}>
          <img src={logo2} alt="文件夹插图" />
        </div>
      </div>

      <div className={styles.rightSection}>
        <div className={styles.registerForm}>
          <h2>立即注册</h2>
          <form onSubmit={handleSubmit}>
            <div className={styles.inputGroup}>
              <label>用户名</label>
              <input
                type="text"
                name="username"
                placeholder="输入您的用户名"
                value={formData.username}
                onChange={handleInputChange}
              />
            </div>

            <div className={styles.inputGroup}>
              <label>电子邮件</label>
              <input
                type="email"
                name="email"
                placeholder="输入您的电子邮件"
                value={formData.email}
                onChange={handleInputChange}
              />
            </div>

            <div className={styles.inputGroup}>
              <label>密码</label>
              <input
                type="password"
                name="password"
                placeholder="输入密码"
                value={formData.password}
                onChange={handleInputChange}
              />
            </div>

            <div className={styles.inputGroup}>
              <label>确认密码</label>
              <input
                type="password"
                name="confirmPassword"
                placeholder="确认密码"
                value={formData.confirmPassword}
                onChange={handleInputChange}
              />
            </div>

            <button type="submit" className={styles.registerButton}>
              创建账户
            </button>
          </form>

          <div className={styles.divider}>
            <span>或</span>
          </div>

          <div className={styles.socialLogin}>
            <button className={styles.wechatButton}>
              <img src={wechat} alt="微信" />
            </button>
          </div>

          <div className={styles.login}>
            <span>已有账户？</span>
            <a href="/login">立即登录</a>
          </div>
        </div>
      </div>
    </div>
  );
}